import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;


/**
 * This is the find/replace function object. It has some simple search function 
 * such as Fuzzy Searching. Combined with Editor.java to perform find, replace 
 * and so on.
 * some of the annotations are added by WANG Jiechao
 * @author Group 3,Zou Longhao
 *
 */
public class FindAndReplace extends JFrame implements ActionListener{
    
    private int index=0;			//index for searching function
    private JTextPane text=null;	//The area for listening
    private JLabel findLabel, replaceLabel;
    private JTextField findTextFiled, replaceTextFiled;
    private JButton findnext, replace,replaceAll;
    
    private boolean isReplace=false;
    private boolean isReplaceAll=false;
    
    private static FindAndReplace fr;
    
    
    FindAndReplace(JTextPane text){//This method is mainly used for creating a frame(dialogue) named
    	                                         //"Find/Replace" initiated by ctrl+f, right click>"Find/Replace"
        super("Find/Replace");                   //or "Edit">"Find/Replace"
        this.text=text;
        findLabel=new JLabel("Find:",SwingConstants.RIGHT);
        replaceLabel=new JLabel("Replace With:",SwingConstants.RIGHT);
        
        findTextFiled=new JTextField(15);
        replaceTextFiled=new JTextField(15);
        findnext=new JButton("Find Next");
        replace=new JButton("Replace");
        replaceAll=new JButton("Replace All");
        
        Listenercollection();
        //Layout
        JPanel panel0=new JPanel(); 
        panel0.setLayout(new GridLayout(2,0,20,20));
        
        JPanel panel1=new JPanel();
        JPanel panel2=new JPanel();
        panel1.setLayout(new GridLayout(2,2,10,10));
        panel2.setLayout(new GridLayout(2,2,10,10));
        
        panel1.add(findLabel);
        panel1.add(findTextFiled);
        panel1.add(replaceLabel);
        panel1.add(replaceTextFiled);
       
        panel2.add(findnext);
        panel2.add(replace);
        panel2.add(replaceAll);
        
        panel0.add(panel1);
        panel0.add(panel2);
        add(panel0);
        
        setSize(300,200);
        setLocation(200, 200);
        setResizable(false);
        setAlwaysOnTop(true);//the dialog is always on top even if it is not selected
        setVisible(true);
        
    }
    //actionPerformed function overwrited by myAction function
    public void actionPerformed(ActionEvent e){
        myAction(e);
    }
    //A listener collection to add listeners
    public void Listenercollection(){
    	findnext.addActionListener(this);
    	replace.addActionListener(this);
    	replaceAll.addActionListener(this);
    }
    //actions function defined by ourselves
    public void myAction(ActionEvent e){
    	if(e.getActionCommand()=="Replace"){
    		this.isReplace=true;//for carrying replace function in the following 
    	}
    	if(e.getActionCommand()=="Replace All"){
    		this.isReplaceAll=true;//for carrying replace all function in the following 
    	}
    
    	String temp=text.getText();//get the text
    	System.out.println(temp);
    	//String temp=textField.getText();
        String find=findTextFiled.getText();//get the keyword for find function
        String replace=replaceTextFiled.getText();//get the keyword for replace function
        
        //This function for Fuzzy Searching.
        String[] sub_string=find.split("\\*");//splitted by "*" to form to strings
   
        int sub_string_number=sub_string.length ;//store the number of elements in the array sub_string
        int sub_index[]=new int[sub_string_number];//create an integer array with length equal to sub_string_number
        
        for(int i=0;i<sub_string_number;i++)
        {   
        	System.out.println(sub_string[i]);//only for testing and debugging
            sub_index[i]=temp.indexOf(sub_string[i],index);//(1)get the index of all the sub_string and store them in array sub_index
            if(sub_index[i]==-1)//if not found
            {           
              index=0;
              break;
            }
            else 
            {
              index=index+sub_string[i].length();//combined with (1) to find the index of the next sub_string
           //   System.out.println(index);
              if(i==(sub_string_number-1))
              {
                //text.setSelectionStart(sub_index[0]);//from sub_index[0] to highlight the corresponding word
                //text.setSelectionEnd(sub_index[0]+find.length());//highlight until sub_index[0]+find.length()
              }
            } 
        }
        
        if(isReplace && temp.indexOf(find)!=-1){		//Replace.
        	text.replaceSelection(replace);//use string in "replace" to replace string in text
        	text.setSelectionStart(index);//from index to perform replace
            text.setSelectionEnd(index+find.length());//perform replace until index+find.length()
        }
        if(isReplaceAll && temp.indexOf(find)!=-1){		//Replace all
        	text.setText(text.getText().replaceAll(find,replace));//use string in "replace" to perform replace all
        	text.setSelectionStart(index);//from index to perform replace all
            text.setSelectionEnd(index+find.length());//perform replace all until index+find.length()
        }
        isReplace=false;//if not found
        isReplaceAll=false;//if not found
    }
    
    //Instantiate the Window of FindAndReplace
//   public static FindAndReplace getInstance(JTextPane text){
  //      if(fr==null){
    //        fr=new FindAndReplace(text);
      //  }
        //return fr;
   // }
}
